﻿var timer = (function () {
    var api = {}
    api.getDateNow = function () {
        var date = new Date()
        var str = ""
        str += date.getFullYear() + "/"
        str += (date.getMonth() + 1) + "/"
        str += date.getDay() + " "
        str += date.getHours() + ":"
        str += date.getMinutes()
        return str;
    }
    return api;
})()

function deleteData(url, id) {
    console.log(arguments.length);
    if (arguments.length == 1) {
        id = iC.getDataString();
        if (id.length == "") {
            swal("请选择要删除的数据！", "", "warning");
            return;
        }
    }
    swal({
        title: "确定要删除?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是",
        cancelButtonText: "否",
        closeOnConfirm: false
    },
    function (ifConfirm) {
        if (ifConfirm)
            deleteFunction(url, id);
    });
}
function deleteFunction(url, id) {
    $.ajax({
        url: url,
        type: "post",
        dataType: "json",
        data: { id: id },
        success: function (data) {
            deleteSuccess(url);
        }, error: function (error) {
            deleteError();
        }
    })
}

function deleteSuccess(url) {
    swal({
        type: "success",
        title: "删除成功！"
    }, function () {
        var location = url.substring(0, url.lastIndexOf("/"));
        window.location.href = location;
    })
}
function deleteError() {
    swal("删除失败！", "", "error");
}

function showError(msg) {
    swal(msg, "", "error");
}
function showSuccess(msg, fu) {
    if (arguments.length == 1) {
        swal(msg, "", "success");
        return;
    }
    swal({
        title: msg,
        type: "success",
    }, function () {
        fu();
    });
}
function confirm(f, msg) {
    var text = msg == undefined ? "确定要删除?" : msg;
    swal({
        title: text,
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是",
        cancelButtonText: "否",
        closeOnConfirm: false
    },
    function (ifConfirm) {
        if (ifConfirm)
            fu();
    });
}
var iC = (function () {
    var _fatherTag;
    var _toggle;
    this.init = function (fatherTag, toggle) {
        _fatherTag = fatherTag;
        _toggle = toggle;
        $('input[type="checkbox"]').iCheck({
            checkboxClass: 'icheckbox_flat-blue',
            radioClass: 'iradio_flat-blue'
        });

        $(toggle).click(function () {
            var clicks = $(this).data('clicks');
            if (clicks) {
                //Uncheck all checkboxes
                $(fatherTag + " input[type='checkbox']").iCheck("uncheck");
                $(".fa", this).removeClass("fa-check-square-o").addClass('fa-square-o');
            } else {
                //Check all checkboxes
                $(fatherTag + " input[type='checkbox']").iCheck("check");
                $(".fa", this).removeClass("fa-square-o").addClass('fa-check-square-o');
            }
            $(this).data("clicks", !clicks);
        });
    }

    this.getData = function () {
        var tags = $(_fatherTag + " input[type='checkbox']");
        var arr = new Array();
        var i, j;
        for (i = 0, j = tags.length; i < j; i++) {
            if (tags.eq(i).parent().hasClass("checked"))
                arr.push({ id: tags.eq(i).attr("data-id") });
        }
        return arr;
    }

    this.getDataString = function () {
        var tags = $(_fatherTag + " input[type='checkbox']");
        var string = "";
        var i, j;
        for (i = 0, j = tags.length; i < j; i++) {
            if (tags.eq(i).parent().hasClass("checked"))
                string += tags.eq(i).attr("data-id") + "|";
        }
        string = string.substring(0, string.length - 1);
        return string;
    }

    return this;
})()

iC.init(".table", "#checkAll");

$(document).ready(function () {
    $("#search").keyDown(function (e) {
        if (e.keyCode == 13) {
            search()
        }
    })

    $(".glyphicon-search").on("click", function () {
        search();
    })
})

function setCover(obj) {
    var cover = '<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>';
    $(obj).html(cover);
}

function GetJsTreeALink(obj) {
    return obj.find(".jstree-anchor").eq(0);
}

//默认是把数据放在a 标签 href属性上面
function GetJsTreeCheckedData(obj) {
    var ids = $(obj).jstree().get_checked();
    var results = new Array(),
        i, j;
    for (i = 0, j = ids.length; i < j; i++) {
        var _id = $("#" + ids[i]).attr("href");
        if (_id != "#")
            results.push($)
    }
    return results;
}

//把数组转化成用|来分割的字符串
function TransformArray(obj) {
    var result = "",
        i, j;
    for (i = 0, j = obj.length; i < j; i++) {
        result += obj[i] + "|";
    }
    return result.substring(0, result.length - 1);
}

//获取当前页面Url
//获取当前页面Url
function GetUrl() {
    var completeUrl = window.location.href;
    //获取参数列表
    if (completeUrl.indexOf('?') == -1)
        return new Array();
    completeUrl = completeUrl.substring(completeUrl.indexOf('?') + 1, completeUrl.length);
    //剔除 # 之后的数据
    var temp = -1;
    completeUrl = completeUrl.substring(0, (temp = completeUrl.indexOf('#')) == -1 ? completeUrl.length : temp);
    return completeUrl.split('&');
}

function AssembleData(arr) {
    var urlData = new Array();
    for (var i = 0, j = arr.length; i < j; i++) {
        var params = arr[i].split('=');
        urlData.push({ key: params[0], value: params[1] });
    }
    return urlData;
}

function setUrlValue(key, value, url) {
    var params;
    if (arguments.length == 3) {
        url = url.substring(1, url.length);
        params = AssembleData(url.split("&"));
    } else {
        params = AssembleData(GetUrl());
    }
    var check = false;
    var paramsString = "?";
    for (var i = 0, j = params.length; i < j; i++) {
        if (params[i].key == key) {
            params[i].value = value;
            paramsString += key + "=" + value;
            check = true;
        } else {
            paramsString += params[i].key + "=" + params[i].value
        }
        paramsString += "&";
    }
    if (!check) {
        return paramsString += key + "=" + value;
    }
    return paramsString.substring(0, paramsString.length - 1);
}

function getUrlValue(key) {
    var params = GetUrl();
    for (var i = 0; i < params.length; i++) {
        var splits = params[i].split('=');
        if (splits[0] == key) {
            return { status: true, msg: splits[1] };
        }
    }
    return { status: false };
}

function getCurrentUrlParams() {
    var url = setUrlValue("", "");
    return url;
}

function checkUrlEffective(url, fu) {
    $.ajax({
        url: url,
        type: "get",
        data: "",
        success: function () {
            fu();
        }, error: function () {
            showError("无效链接！");
        }
    })
}


